package com.cskaoyan.service.impl;

import com.cskaoyan.bean.GenericData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketStorage;
import com.cskaoyan.bean.po.MarketStorageExample;
import com.cskaoyan.bean.storage.Storage;
import com.cskaoyan.constant.Constant;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.service.StorageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author LiangRuiJiang
 * @since 2022/06/05  19:25
 */
@Service
public class StorageServicceImpl implements StorageService {

    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    @Transactional
    public MarketStorage createFile(MultipartFile file, HttpServletRequest request) {
        String filename = file.getOriginalFilename();
        String substring = null;
        if (filename != null) {
            int index = filename.lastIndexOf(".");
            substring = filename.substring(index);
        }
        String key = UUID.randomUUID().toString() + substring;
        long size = file.getSize();
        String contentType = file.getContentType();
        //
        String requestURL = Constant.DOMAIN + Constant.FILE_ACCESS_PATH_PATTERN + key;

        MarketStorage marketStorage = new MarketStorage(null, key, filename, contentType, size, requestURL, new Date(), new Date(), null);
        try {
            marketStorageMapper.insertSelective(marketStorage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return marketStorage;
    }

    @Override
    public GenericData<MarketStorage> queryStorage(BaseParam param, String key, String name) {
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();
        String nameCondition = null;
        if (StringUtils.isEmpty(name)) {
            nameCondition = "%%";
        }else {
            nameCondition = "%" + name + "%";
        }
        criteria.andNameLike(nameCondition);
        if (key != null) {
            criteria.andKeyEqualTo(key);
        }
        //没被删除
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(param.getSort() + " " +param.getOrder());
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);

        PageInfo pageInfo = new PageInfo(marketStorages);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        Integer page = param.getPage();
        Integer limit = param.getLimit();
        return GenericData.data(total, pages, limit, page, marketStorages);
    }

    @Override
    @Transactional
    public MarketStorage updateStorage(MarketStorage marketStorage) {
        marketStorage.setUpdateTime(new Date());
        marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
        MarketStorage marketStorageNew = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());
        return marketStorageNew;
    }

    @Override
    @Transactional
    public void deleteStorage(MarketStorage marketStorage) {
        marketStorage.setDeleted(true);
        marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
    }
/**
 * @Description:小程序保存图片
  * @param
 * @return:
 * @Author: 蒋镇泽
 * @Date: 2022/6/7 22:02
 */
    @Override
    public void storageCreate(MarketStorage marketStorage) {
        marketStorageMapper.insertSelective(marketStorage);

    }
}
